/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ui;

import biz.DeparmentProgess;
import biz.LoginProgess;
import biz.RoleProgess;
import da.exceptions.PreexistingEntityException;
import da.exceptions.RollbackFailureException;
import entities.Account;
import entities.Department;
import entities.Role;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

/**
 *
 * @author TungGreen
 */
@ManagedBean
@SessionScoped
public class UserProgess {

    @EJB
    private RoleProgess roleProgess;
    @EJB
    private LoginProgess loginProgess;
    @EJB
    private DeparmentProgess deparmentProgess;
    private int roleId;
    private int departmentId;
    private String fullName;
    private String address;
    private int phone;
    private String userName;
    private String password;
    private String rePassword;
    private Role role;
    private Department department;
    private String msg;
    private Account user;
    private String checkName;
    private String phoneTest;
    private Account newUser;
    private boolean check = false;

    /**
     * Xoa param
     */
    public void checkClear() {
        boolean temp = check;
        if (temp == true) {
            check = false;
            clear();
        }
    }

    /**
     * Function reset all text
     *
     */
    public void clear() {
        fullName = null;
        address = null;
        phoneTest = null;
        userName = null;
        password = null;
        msg = null;
    }

    /**
     * Function create new Account
     */
    public String createUser() {
        //get roleID
        role = roleProgess.getRole(roleId);
        //get department
        department = deparmentProgess.getDepartment(departmentId);
        user = null;
        //get account login
        user = loginProgess.getUser(userName);
        //Validate Account
        try {
            checkName = user.getFullName();
        } catch (Exception e) {
            checkName = null;
        }
        /**
         * Validate all information
         */
        if ("".equals(fullName)) {
            msg = "Input FullName please!";
            return "";
        }
        if ("".equals(address)) {
            msg = "Input Address please!";
            return "";
        }
        if (phoneTest == null) {
            msg = "Input Phone please!";
            return "";
        }
        if ("".equals(userName)) {
            msg = "Input Username please! ";
            return "";
        }
        if ("".equals(password)) {
            msg = "Input Password please!";
            return "";
        }
        if (!password.equals(rePassword)) {
            msg = "Re-Password wrong!";
            return "";
        }
        //Create new account 
        if (checkName == null) {
            try {
                phone = Integer.parseInt(phoneTest);
                Account ac = new Account(1);
                ac.setId(1);
                ac.setFullName(fullName);
                ac.setAddress(address);
                ac.setPhoneNo(phone);
                ac.setUserName(userName);
                ac.setPassWord(password);
                ac.setRoleId(role);
                ac.setDepartmentId(department);
                //call ejb to create new account
                loginProgess.createUser(ac);
                newUser = ac;
                check = true;
                return "new_user.xhtml";
            } catch (PreexistingEntityException ex) {
                Logger.getLogger(UserProgess.class.getName()).log(Level.SEVERE, null, ex);
            } catch (RollbackFailureException ex) {
                Logger.getLogger(UserProgess.class.getName()).log(Level.SEVERE, null, ex);
            } catch (Exception ex) {
                Logger.getLogger(UserProgess.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else {
            msg = "Username has already!";
            return "";
        }
        return "";
    }

    /**
     * redirect to create user page
     * @return
     */
    public String goCreate() {
        clear();
        return "create_user.xhtml";
    }

    public Account getNewUser() {
        return newUser;
    }

    public void setNewUser(Account newUser) {
        this.newUser = newUser;
    }

    public String getPhoneTest() {
        return phoneTest;
    }

    public void setPhoneTest(String phoneTest) {
        this.phoneTest = phoneTest;
    }

    public RoleProgess getRoleProgess() {
        return roleProgess;
    }

    public void setRoleProgess(RoleProgess roleProgess) {
        this.roleProgess = roleProgess;
    }

    public LoginProgess getLoginProgess() {
        return loginProgess;
    }

    public void setLoginProgess(LoginProgess loginProgess) {
        this.loginProgess = loginProgess;
    }

    public DeparmentProgess getDeparmentProgess() {
        return deparmentProgess;
    }

    public void setDeparmentProgess(DeparmentProgess deparmentProgess) {
        this.deparmentProgess = deparmentProgess;
    }

    public int getRoleId() {
        return roleId;
    }

    public void setRoleId(int roleId) {
        this.roleId = roleId;
    }

    public int getDepartmentId() {
        return departmentId;
    }

    public void setDepartmentId(int departmentId) {
        this.departmentId = departmentId;
    }

    public String getFullName() {
        return fullName;
    }

    public void setFullName(String fullName) {
        this.fullName = fullName;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public int getPhone() {
        return phone;
    }

    public void setPhone(int phone) {
        this.phone = phone;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getRePassword() {
        return rePassword;
    }

    public void setRePassword(String rePassword) {
        this.rePassword = rePassword;
    }

    public Role getRole() {
        return role;
    }

    public void setRole(Role role) {
        this.role = role;
    }

    public Department getDepartment() {
        return department;
    }

    public void setDepartment(Department department) {
        this.department = department;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Account getUser() {
        return user;
    }

    public void setUser(Account user) {
        this.user = user;
    }

    public String getCheckName() {
        return checkName;
    }

    public void setCheckName(String checkName) {
        this.checkName = checkName;
    }
}
